package com.guinea.account.service;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import com.guinea.account.dao.DictionaryDao;
import com.guinea.account.entity.Dictionary;
import com.guinea.core.orm.hibernate.HibernateDao;
import com.guinea.core.service.CRUDManager;

/**
 * @author huaxiaz
 */
public class DictionaryManager extends CRUDManager {
	private static final String QUERY_CHILDREN_BY_PARENTID = "select d from com.guinea.account.entity.Dictionary d where d.parentId=?";

	private DictionaryDao dictionaryDao;

	public HibernateDao getEntityDao() {
		return getDictionaryDao();
	}

	public Dictionary getDictionary(Serializable id) {
		return (Dictionary)getDictionaryDao().get(id);
	}

	public DictionaryDao getDictionaryDao() {
		return dictionaryDao;
	}

	public void setDictionaryDao(DictionaryDao dictionaryDao) {
		this.dictionaryDao = dictionaryDao;
	}

	public List getChildren(Serializable id){
		return getDictionaryDao().createQuery(QUERY_CHILDREN_BY_PARENTID,id).list();
	}
	public String[] getChildrenIds(Serializable id){
		List children = getChildren(id);
		String[] childrenIds = new String[children.size()];
		int i = 0;
		for (Iterator iter = children.iterator(); iter.hasNext();i++) {
			Dictionary child = (Dictionary) iter.next();
			childrenIds[i] = child.getId().toString();
		}
		return childrenIds;
	}
	
	public List getRootNodes(){
		return getDictionaryDao().find("select d from Dictionary d where d.parentId = 0");
	}
	
	public List getLeafNodes(){
		return getDictionaryDao().find("select d from Dictionary d where d.parentId != 0");
	}
	
}
